import {Component, OnDestroy, OnInit} from '@angular/core';
import {DeviceService} from '../../svc/device.svc';

@Component({
    selector: 'app-system-information',
    templateUrl: './system-information.component.html',
    styleUrls: ['./system-information.component.css']
})
export class SystemInformationComponent implements OnInit, OnDestroy {
    networkTableStatus: boolean;

    systemInformation = {
        dModel: '',
        serialNumber: '',
        workingMode: '',
        webVersion: '',
        BCGVersion: ''
    };

    BCGInformation = {
        status: '',     // 新
        heartRate: '',
        heartRateVariability: '',    // 新
        respirationRate: '',
        strokeVolume: '',   // 新
        calibrationParameters: '',
        measurementDirection: '',
    };

    WLANAPInformation = {
        SSID: '',
        channel: '',
        security: ''
    };
    WLANClientInformation = {
        SSID: '',
        security: '',
        DHCP: 'Empty',
        ipAddress: '',
        netmask: '',
        gateway: '',
        primaryDNS: '',
        secondaryDNS: '',
    };

    networkInformation = {
        dhcp: 'Empty',
        ssid: '',
        protocol: '',
        securityType: '',
        networkBand: '',
        networkChannel: '',
        ipv4Address: '',
        dns: '',
    };
    bcgTimer;

    constructor(public ds: DeviceService) {
    }

    ngOnInit(): void {
        this.networkTableSwitch();
        this.getDeviceInformationData();
        this.getWLANAPData();
        this.getWLANClientData();
        const that = this;
        this.bcgTimer = setInterval(() => {
            that.getBCGData();
        }, 1000);
    }

    ngOnDestroy(): void {
        clearInterval(this.bcgTimer);
    }

    networkTableSwitch(): void {
        this.networkTableStatus = this.WLANClientInformation.DHCP !== 'DHCP';
    }

    getDeviceInformationData(): void {
        this.ds.getSystemInformationData().subscribe(data => {
            this.systemInformation = data;
        });
    }

    getBCGData(): void {
        this.ds.getBCGData().subscribe(data => {
            this.BCGInformation.heartRate = data.HeartRate;
            this.BCGInformation.respirationRate = data.BreathRate;
            this.BCGInformation.heartRateVariability = data.HRV;
            this.BCGInformation.strokeVolume = data.SV;
            switch (data.MotionDetected) {
                case 0: this.BCGInformation.status = 'Empty'; break;
                case 1: this.BCGInformation.status = 'Occupied'; break;
                case 2: this.BCGInformation.status = 'Moving'; break;
                default: this.BCGInformation.status = 'Unknown';
            }
        });
    }

    getWLANAPData(): void {
        this.ds.getWLANAPData().subscribe(data => {
            this.WLANAPInformation = data;
        });
    }

    getWLANClientData(): void {
        this.ds.getStatusJson().subscribe(data => {
            this.networkInformation.dhcp = 'Empty';
            this.networkInformation.ssid = data.ssid;
            this.networkInformation.ipv4Address = data.ip;
        });
    }

    rebootToComplete(): void {
        this.ds.rebootDevice().subscribe(data => {
           console.log('Rebooting...');
        });
    }
}
